<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Resource Autoloaders - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.loader.autoloader-resource.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.loader.autoloader-resource.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.loader.autoloader.html">The Autoloader</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.loader.html">Zend_Loader</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.loader.pluginloader.html">Loading Plugins</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.loader.autoloader-resource" class="section"><div class="info"><h1 class="title">Resource Autoloaders</h1></div>
    

    <p class="para">
        Resource autoloaders are intended to manage namespaced library code that
        follow Zend Framework coding standard guidelines, but which do not have
        a 1:1 mapping between the class name and the directory structure. Their
        primary purpose is to facilitate autoloading application resource code,
        such as application-specific models, forms, and <acronym class="acronym">ACL</acronym>s.
    </p>

    <p class="para">
        Resource autoloaders register with the <a href="zend.loader.autoloader.html" class="link">autoloader</a> on instantiation,
        with the namespace to which they are associated. This allows you to
        easily namespace code in specific directories, and still reap the
        benefits of autoloading.
    </p>

    <div class="section" id="zend.loader.autoloader-resource.usage"><div class="info"><h1 class="title">Resource autoloader usage</h1></div>
        

        <p class="para">
            Let&#039;s consider the following directory structure:
        </p>

        <pre class="programlisting brush: text">
path/to/some/directory/
    acls/
        Site.php
    forms/
        Login.php
    models/
        User.php
</pre>


        <p class="para">
            Within this directory, all code is prefixed with the namespace
            &quot;My_&quot;. Within the &quot;acls&quot; subdirectory, the component prefix &quot;Acl_&quot;
            is added, giving a final class name of &quot;My_Acl_Site&quot;. Similarly, the
            &quot;forms&quot; subdirectory maps to &quot;Form_&quot;, giving &quot;My_Form_Login&quot;. The
            &quot;models&quot; subdirectory maps to &quot;Model_&quot;, giving &quot;My_Model_User&quot;.
        </p>

        <p class="para">
            You can use a resource autoloader to autoload these classes. To
            instantiate the resource autoloader, you are required to pass at the
            minimum the base path and namespace for the resources it will be
            responsible for:
        </p>

        <pre class="programlisting brush: php">
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
    &#039;basePath&#039;  =&gt; &#039;path/to/some/directory&#039;,
    &#039;namespace&#039; =&gt; &#039;My&#039;,
));
</pre>


        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Base namespace</b><br /></span>
            

            <p class="para">
                In <span class="classname">Zend_Loader_Autoloader</span>, you are expected to
                provide the trailing underscore (&quot;_&quot;) in your namespace if your
                autoloader will use it to match the namespace.
                <span class="classname">Zend_Loader_Autoloader_Resource</span> makes the
                assumption that all code you are autoloading will use an
                underscore separator between namespaces, components, and
                classes. As a result, you do not need to use the trailing
                underscore when registering a resource autoloader.
            </p>
        </p></blockquote>

        <p class="para">
            Now that we have setup the base resource autoloader, we can add some
            components to it to autoload. This is done using the
             <span class="methodname">addResourceType()</span> method, which accepts three
            arguments: a resource &quot;type&quot;, used internally as a reference name;
            the subdirectory path underneath the base path in which these
            resources live; and the component namespace to append to the base
            namespace. As an example, let&#039;s add each of our resource types.
        </p>

        <pre class="programlisting brush: php">
$resourceLoader-&gt;addResourceType(&#039;acl&#039;, &#039;acls/&#039;, &#039;Acl&#039;)
               -&gt;addResourceType(&#039;form&#039;, &#039;forms/&#039;, &#039;Form&#039;)
               -&gt;addResourceType(&#039;model&#039;, &#039;models/&#039;, &#039;Model&#039;);
</pre>


        <p class="para">
            Alternately, you could pass these as an array to
             <span class="methodname">addResourceTypes()</span>; the following is equivalent to the
            above:
        </p>

        <pre class="programlisting brush: php">
$resourceLoader-&gt;addResourceTypes(array(
    &#039;acl&#039; =&gt; array(
        &#039;path&#039;      =&gt; &#039;acls/&#039;,
        &#039;namespace&#039; =&gt; &#039;Acl&#039;,
    ),
    &#039;form&#039; =&gt; array(
        &#039;path&#039;      =&gt; &#039;forms/&#039;,
        &#039;namespace&#039; =&gt; &#039;Form&#039;,
    ),
    &#039;model&#039; =&gt; array(
        &#039;path&#039;      =&gt; &#039;models/&#039;,
        &#039;namespace&#039; =&gt; &#039;Model&#039;,
    ),
));
</pre>


        <p class="para">
            Finally, you can specify all of this when instantiating the object,
            by simply specifying a &quot;resourceTypes&quot; key in the options passed and
            a structure like that above:
        </p>

        <pre class="programlisting brush: php">
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
    &#039;basePath&#039;      =&gt; &#039;path/to/some/directory&#039;,
    &#039;namespace&#039;     =&gt; &#039;My&#039;,
    &#039;resourceTypes&#039; =&gt; array(
        &#039;acl&#039; =&gt; array(
            &#039;path&#039;      =&gt; &#039;acls/&#039;,
            &#039;namespace&#039; =&gt; &#039;Acl&#039;,
        ),
        &#039;form&#039; =&gt; array(
            &#039;path&#039;      =&gt; &#039;forms/&#039;,
            &#039;namespace&#039; =&gt; &#039;Form&#039;,
        ),
        &#039;model&#039; =&gt; array(
            &#039;path&#039;      =&gt; &#039;models/&#039;,
            &#039;namespace&#039; =&gt; &#039;Model&#039;,
        ),
    ),
));
</pre>

    </div>

    <div class="section" id="zend.loader.autoloader-resource.module"><div class="info"><h1 class="title">The Module Resource Autoloader</h1></div>
        

        <p class="para">
            Zend Framework ships with a concrete implementation of
            <span class="classname">Zend_Loader_Autoloader_Resource</span> that contains resource
            type mappings that cover the default recommended directory structure
            for Zend Framework <acronym class="acronym">MVC</acronym> applications. This loader,
            <span class="classname">Zend_Application_Module_Autoloader</span>, comes with the
            following mappings:
        </p>

        <pre class="programlisting brush: text">
forms/       =&gt; Form
models/      =&gt; Model
    DbTable/ =&gt; Model_DbTable
    mappers/ =&gt; Model_Mapper
plugins/     =&gt; Plugin
services/    =&gt; Service
views/
    helpers  =&gt; View_Helper
    filters  =&gt; View_Filter
</pre>


        <p class="para">
            As an example, if you have a module with the prefix of &quot;Blog_&quot;, and
            attempted to instantiate the class &quot;Blog_Form_Entry&quot;, it would look
            in the resource directory&#039;s &quot;forms/&quot; subdirectory for a file named
            &quot;Entry.php&quot;.
        </p>

        <p class="para">
            When using module bootstraps with <span class="classname">Zend_Application</span>, an
            instance of <span class="classname">Zend_Application_Module_Autoloader</span> will be
            created by default for each discrete module, allowing you to
            autoload module resources.
        </p>
    </div>

    <div class="section" id="zend.loader.autoloader-resource.factory"><div class="info"><h1 class="title">Using Resource Autoloaders as Object Factories</h1></div>
        
        <p class="para"/>
        
    </div>

    <div class="section" id="zend.loader.autoloader-resource.reference"><div class="info"><h1 class="title">Resource Autoloader Reference</h1></div>
        
        <p class="para"/>
        
    </div>

    
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.loader.autoloader.html">The Autoloader</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.loader.html">Zend_Loader</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.loader.pluginloader.html">Loading Plugins</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.loader.html">Zend_Loader</a></li>
  <li><a href="zend.loader.load.html">Loading Files and Classes Dynamically</a></li>
  <li><a href="zend.loader.autoloader.html">The Autoloader</a></li>
  <li class="active"><a href="zend.loader.autoloader-resource.html">Resource Autoloaders</a></li>
  <li><a href="zend.loader.pluginloader.html">Loading Plugins</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>